package com.peenongsaban.freelance.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateFormatUtils {

	public static synchronized Date stringToDate_yyyMMdd(String temp) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(temp);
		return date;
	}
	
	public static synchronized String dateToString_yyyyMMdd(Date d) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(d);
		return date;
	}
	
	public static synchronized String getDateNowThaiFormat() {
		Date date = new Date();
		SimpleDateFormat simple_date = new SimpleDateFormat("dd MMMM yyyy",	new Locale("th", "th"));
		String date_str = simple_date.format(date);
		return date_str;
	}
	
	public static synchronized String getDateThaiFormat(Date date) {
		String date_str = "";
		if(date != null){
			SimpleDateFormat simple_date = new SimpleDateFormat("d MMMM yyyy", new Locale("th", "th"));
			date_str = simple_date.format(date);
			return date_str;
		}
		return date_str;
	}
	
	public static synchronized String getDateThaiFormat2(Date date) {
		String date_str = "";
		if(date != null){
			SimpleDateFormat simple_date = new SimpleDateFormat("dd/MM/yyyy");
			date_str = simple_date.format(date);
			return date_str;
		}
		return date_str;
	}
	
	public static synchronized String getTimestampThaiFormatFull(Timestamp timestamp) {
		String date_str = "";
		if(timestamp != null){
			Date date = new Date(timestamp.getTime());
			SimpleDateFormat simple_date = new SimpleDateFormat("วันที่ d เดือนMMMM พุทธศักราช yyyy เวลา HH:mm:ss", new Locale("th", "th"));
			date_str = simple_date.format(date);
		}
		return date_str;
	}
	
	public static synchronized String getDateThaiFormatFull(Date date) {
		String date_str = "";
		if(date != null){
			SimpleDateFormat simple_date = new SimpleDateFormat("วันที่ d เดือนMMMM พุทธศักราช yyyy", new Locale("th", "th"));
			date_str = simple_date.format(date);
		}
		return date_str;
	}
	
	public static synchronized String getDateThaiFormatFull() {
		Date date = new Date();
		SimpleDateFormat simple_date = new SimpleDateFormat("วันที่ d เดือนMMMM พุทธศักราช yyyy",	new Locale("th", "th"));
		String date_str = simple_date.format(date);
		return date_str;
	}
	
	public static synchronized Timestamp getTimeStamp(){
		java.util.Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		return timestamp;
	}
	
	public static synchronized java.sql.Date getDate(){
		java.util.Calendar cal = java.util.Calendar.getInstance(new Locale("th", "th"));
		java.util.Date utilDate = cal.getTime();
		java.sql.Date date = new java.sql.Date(utilDate.getTime());
		return date;
	}
	
	public static synchronized int calAge(Date date_param){		
		try{
			Calendar calendar = Calendar.getInstance(new Locale("th", "th"));  
	        calendar.setTime(date_param);  
	        int year = calendar.get(Calendar.YEAR);
	        
	        Calendar calendar2 = Calendar.getInstance(new Locale("th", "th")); 
	        int year_now = calendar2.get(Calendar.YEAR);
	        
			return year_now - year;
		}catch(Exception ex){
			ExceptionUtil.messageException(new Throwable(), ex);
		}		
		
		return 0;
	}
	
	public static synchronized int calYear(String year_txt){			
		try{
	        int year = Integer.parseInt(year_txt);
	        
	        Calendar calendar = Calendar.getInstance(new Locale("th", "th")); 
	        int year_now = calendar.get(Calendar.YEAR);
	        
			return year_now - year;
		}catch(Exception ex){
			ExceptionUtil.messageException(new Throwable(), ex);
		}		
		
		return 0;
	}
	
	public static synchronized boolean isDateBetween(Date date1, Date date2){			
		try{
			Calendar calendar1 = Calendar.getInstance(new Locale("th", "th"));  
	        calendar1.setTime(date1);
	        
	        Calendar calendar2 = Calendar.getInstance(new Locale("th", "th"));  
	        calendar2.setTime(date2);
	        
	        Calendar now = Calendar.getInstance(new Locale("th", "th"));
	        
	        if((now.after(calendar1) || now.equals(calendar1)) && (now.before(calendar2) || now.equals(calendar2))){
	        	return true;
	        }
		}catch(Exception ex){
			ExceptionUtil.messageException(new Throwable(), ex);
		}
		return false;
	}
	
	public static synchronized java.sql.Date dateUtilToDateSQL(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime());
		return sqlDate;
	}
}
